﻿/******************************************************************************
 * 创建者：gtts
 * 创建日期：2011.4.9
 * 类名：Word
 * 说明：操作Word文档
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSWord = Microsoft.Office.Interop.Word;
using System.Diagnostics;

namespace GTTSOfLibray4.OfficeManager
{
    /// <summary>
    /// 操作Word文档类
    /// </summary>
    public class WordOperate : IDisposable
    {
        #region 字段

        private MSWord.Application m_WordApp = null;
        private object missing = System.Reflection.Missing.Value;
        private MSWord.Document m_Document = null;

        #endregion

        #region 构造函数和析构函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public WordOperate()
        {
            m_WordApp = new MSWord.Application();
        }
        /// <summary>
        /// 析构函数
        /// </summary>
        ~WordOperate()
        {
            Dispose();
        }

        #endregion

        #region 基本操作

        /// <summary>
        /// 创建Word 2003文档
        /// </summary>
        /// <param name="filePath">新建文件的地址</param>
        /// <returns>是否成功</returns>
        public bool CreateDocument(string filePath)
        {
            object path = filePath;
            //截取文件名
            string fileName = filePath.Substring(filePath.LastIndexOf('/') + 1);

            //参数检验
            if (string.IsNullOrEmpty(filePath))
            {
                throw new NullReferenceException("请输入文件路径");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("请输入文件名称");
            }
            if (!fileName.Contains('.') ||
                fileName.Substring(fileName.LastIndexOf('.') + 1) != "doc"
                )
            {
                throw new FormatException("文件名不合法");
            }

            //初始化Word对象
            m_Document = m_WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            object format = MSWord.WdSaveFormat.wdFormatDocument;

            //保存文档
            m_Document.SaveAs(
                ref path,
                ref format,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing);

            return true;
        }

        /// <summary>
        /// 打开Word 2003文档
        /// </summary>
        /// <param name="fileName">文件路径</param>
        public void OpenDocument(string fileName)
        {
            this.OpenDocument(fileName, false);
        }
        /// <summary>
        /// 打开Word 2003文档
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="isReadOnly">是否只读</param>
        public void OpenDocument(string fileName, bool isReadOnly)
        {
            //参数检验
            if (string.IsNullOrEmpty(fileName))
            {
                throw new NullReferenceException("请输入文件路径");
            }
            object obj_FileName = fileName;
            object obj_ReadOnly = isReadOnly;

            //初始化Word对象
            m_Document = m_WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            //打开word
            m_Document = m_WordApp.Documents.Open(
                ref obj_FileName,
                ref missing,
                ref obj_ReadOnly,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing);
        }

        /// <summary>
        /// 保存Word 2003文档
        /// </summary>
        public void Save()
        {
            if (m_Document != null)
            {
                m_Document.Save();
            }
        }

        /// <summary>
        /// 另存为Word 2003文档
        /// </summary>
        /// <param name="fileName">新路径</param>
        public void Saveas(string fileName)
        {
            //参数检验
            if (string.IsNullOrEmpty(fileName))
            {
                throw new NullReferenceException("请输入文件路径");
            }

            object objFileName = fileName;
            if (m_Document != null)
            {
                m_Document.SaveAs(
                    ref objFileName,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing);
            }
        }

        //关闭
        public void Close(bool isSaveChanges)
        {
            object saveChanges = MSWord.WdSaveOptions.wdDoNotSaveChanges;
            if (isSaveChanges)
            {
                saveChanges = MSWord.WdSaveOptions.wdSaveChanges;
            }
            if (m_Document != null)
            {
                Dispose();
            }
        }


        #region 添加数据
        //向word中写入文本数据

        #endregion
        //打印
        //搜索和替换
        //表格
        //文档中的文本和对象
        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            try
            {
                if (m_WordApp != null)
                {
                    m_WordApp.Quit(ref missing, ref missing, ref missing);
                }
                if (m_Document != null)
                {
                    m_Document.Close(ref missing, ref missing, ref missing);
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
            }
        }

        #endregion
    }
}
